package com.longrise.android.byjk.plugins.tabsecond;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.AllCourseChildBean;
import com.longrise.android.byjk.utils.TimeHelper;
import com.longrise.common.utils.AppUtil;

import java.util.List;

/**
 * Created by callxkj on 2017/10/23.
 * 保险医学 每个系统的rcvadapter
 */

public class SecondDetailAdapter extends BaseQuickAdapter<AllCourseChildBean, BaseViewHolder> {


    private OnDetailItemClickListener mListener;

    public SecondDetailAdapter() {
        super(R.layout.item_allcourse_child);
    }

    public void setData(List<AllCourseChildBean> childBeanList) {
        setNewData(childBeanList);
    }

    @Override
    protected void convert(BaseViewHolder helper, final AllCourseChildBean childBean) {
        final View convertView = helper.getConvertView();

        ImageView iv = helper.getView(R.id.item_allcourse_iv);
        //图片
        String realpic = childBean.getRealpic();
        Glide.with(mContext)
                .load(realpic)
                .placeholder(mContext.getResources().getDrawable(R.drawable.img03))
                .crossFade()
                .into(iv);
        //标题
        String name = childBean.getName();
        helper.setText(R.id.item_allcourse_tv1, name);
        final TextView tv_title = helper.getView(R.id.item_allcourse_tv1);

        tv_title.post(new Runnable() {
            @Override
            public void run() {
                int lineCount = tv_title.getLineCount();
                ViewGroup.LayoutParams layoutParams = convertView.getLayoutParams();
                if (lineCount == 2) {
                    layoutParams.height = AppUtil.dip2px(126);
                } else {
                    layoutParams.height = AppUtil.dip2px(107);
                }
                convertView.setLayoutParams(layoutParams);
            }
        });

        //课程价格(折后价)
        String realprice = childBean.getRealprice();
        if ("0.00".equals(realprice)) {
            helper.setText(R.id.item_allcourse_money, "免费");
        } else {
            helper.setText(R.id.item_allcourse_money, "￥ " + realprice);
        }

        //课程时常
        String grouptime = childBean.getGrouptime();
        grouptime = TimeHelper.formateTime(grouptime);
        helper.setText(R.id.item_allcourse_time, grouptime);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    String courseid = childBean.getCourseid();
                    String realprice1 = childBean.getRealprice();
                    mListener.onClick(courseid,realprice1);
                }
            }
        });

    }

    public void setOnDetailItemClickListener(OnDetailItemClickListener listener) {
        this.mListener = listener;
    }

    interface OnDetailItemClickListener {
        void onClick(String courseid, String realprice);
    }

}
